//
//  JLCreatePersonMegViewController.m
//  JLStyleApp
//
//  Created by LvJianfeng on 15/8/21.
//  Copyright (c) 2015年 JLStyle. All rights reserved.
//

#import "JLCreatePersonMegViewController.h"
#import "JLCityTableViewController.h"
#import "JLCreateJobMegViewController.h"

@interface JLCreatePersonMegViewController () <UIScrollViewDelegate, PopActionSheetDelegate, JLCityPassDelegate>
{
    int clickTag;
    City *tempCity;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UILabel *sexLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UILabel *xueliLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *xueliTextField;

@property (weak, nonatomic) AgeView *ageView;
@end

@implementation JLCreatePersonMegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // self.clearsSelectionOnViewWillAppear = NO;
    self.title = @"我的信息";
    self.view.backgroundColor = [UIColor customControllerBackgroundColor];
    EmtryCellHidden(self.tableView);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    
    [self storyboardUIInit];
    
    [self setupRightBarItem];
}

- (void)nextAction{
    JLCreateJobMegViewController *cjmvc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass([JLCreateJobMegViewController class])];
    [self.navigationController pushViewController:cjmvc animated:YES];
}

- (void)confirmDateAction{
    self.ageLabel.text = [NSString stringWithFormat:@"%d", [JFGlobalInstance DateConvertToAge:[self.ageView.datePicker date]]];
    if (![self.ageLabel.text isEqualToString:@"点击选择"]) {
        self.ageLabel.textColor = [UIColor darkGrayColor];
    }else{
        self.ageLabel.textColor = [UIColor lightGrayColor];
    }
    [self.ageView tappedCancel];
}

#pragma mark UITabelViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self.nameTextField becomeFirstResponder];
            break;
        case 1:
        {
            PopActionSheet *sexSheet = [PopActionSheet popSheetWithTitle:nil
                                                            buttonTitles:@[@"男", @"女", @"保密"]
                                                          redButtonIndex:-1
                                                                delegate:self];
            clickTag = 1;
            [sexSheet show];
        }
            break;
        case 2:
        {
            [self.view endEditing:YES];
            AgeView *ageView = [[AgeView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            [ageView.confirmButton addTarget:self action:@selector(confirmDateAction) forControlEvents:UIControlEventTouchUpInside];
            [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:ageView];
            self.ageView = ageView;
        }
            
            break;
        case 3:
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                JLCityTableViewController *cityTableViewController = [[JLCityTableViewController alloc] init];
                UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:cityTableViewController];
                [navigationController.navigationBar setCustomBackgroundColor:GlobalColor];
                navigationController.navigationBar.tintColor = [UIColor whiteColor];
                [navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,JLBlodFont(17),NSFontAttributeName,nil]];
                cityTableViewController.delegate = self;
                [self presentViewController:navigationController animated:YES completion:nil];
            });
        }
            break;
        case 4:{
            PopActionSheet *xueliSheet = [PopActionSheet popSheetWithTitle:@"Tip:点击其他可以自己创建哦."
                                                            buttonTitles:@[@"大专", @"本科", @"博士", @"硕士", @"其他"]
                                                          redButtonIndex:-1
                                                                delegate:self];
            clickTag = 4;
            [xueliSheet show];
        }
            break;
        case 5:
            [self.phoneTextField becomeFirstResponder];
            break;
        case 6:
            [self.emailTextField becomeFirstResponder];
            break;
            
        default:
            break;
    }
}

-(void)actionSheet:(PopActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (clickTag==1) {
        switch (buttonIndex) {
            case 0:
                self.sexLabel.text = @"男";
                break;
            case 1:
                self.sexLabel.text = @"女";
                break;
            case 2:
                self.sexLabel.text = @"保密";
                break;
        }
        if (![self.sexLabel.text isEqualToString:@"点击选择"]) {
            self.sexLabel.textColor = [UIColor darkGrayColor];
        }else{
            self.sexLabel.textColor = [UIColor lightGrayColor];
        }
    }
    if (clickTag==4) {
        switch (buttonIndex) {
            case 0:
                self.xueliLabel.text = @"大专";
                break;
            case 1:
                self.xueliLabel.text = @"本科";
                break;
            case 2:
                self.xueliLabel.text = @"博士";
                break;
            case 3:
                self.xueliLabel.text = @"硕士";
                break;
            case 4:
                //self.xueliLabel.text = @"其他";
                self.xueliLabel.hidden = YES;
                self.xueliTextField.hidden = NO;
                [self.xueliTextField becomeFirstResponder];
                break;
        }
        if (![self.xueliLabel.text isEqualToString:@"点击选择"]) {
            self.xueliLabel.textColor = [UIColor darkGrayColor];
        }else{
            self.xueliLabel.textColor = [UIColor lightGrayColor];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.view endEditing:YES];
}

#pragma mark ChooseDelegate
- (void)cityPass:(City *)value{
    tempCity = value;
    self.cityLabel.text = tempCity.name;
    
    if (![self.cityLabel.text isEqualToString:@"点击选择"]) {
        self.cityLabel.textColor = [UIColor darkGrayColor];
    }else{
        self.cityLabel.textColor = [UIColor lightGrayColor];
    }
}

- (void)storyboardUIInit{
    self.phoneTextField.keyboardType = UIKeyboardTypePhonePad;
    self.emailTextField.keyboardType = UIKeyboardTypeEmailAddress;
    
    self.nameTextField.placeholderColor  = [UIColor lightGrayColor];
    self.phoneTextField.placeholderColor = [UIColor lightGrayColor];
    self.emailTextField.placeholderColor = [UIColor lightGrayColor];
    self.xueliTextField.placeholderColor = [UIColor lightGrayColor];
}

- (void)setupRightBarItem{
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    doneButton.titleLabel.font = JLFont(15);
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor customLightPlaceColor] forState:UIControlStateHighlighted];
    [doneButton setTitle:@"下一步" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

@implementation AgeView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.15];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        UIView *toolBarView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 250)];
        toolBarView.backgroundColor = [UIColor customControllerBackgroundColor];
        [self addSubview:toolBarView];
        
        UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-60, 0, 40, 40)];
        [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor customSystemColor] forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        confirmButton.titleLabel.font = JLBlodFont(16);
        [toolBarView addSubview:confirmButton];
        self.confirmButton = confirmButton;
        
        UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 0, 40, 40)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor customSystemColor] forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [cancelButton addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.titleLabel.font = JLBlodFont(16);
        [toolBarView addSubview:cancelButton];
        
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 30, ScreenWidth, 200)];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.locale = locale;
        [toolBarView addSubview:datePicker];
        self.datePicker = datePicker;
        
        [UIView animateWithDuration:0.4 animations:^{
            [toolBarView setFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
        } completion:^(BOOL finished) {
        }];
    }
    return self;
}

- (void)tappedCancel
{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}
@end
